<template>
  <div class="app-container">

    <div class="search-container">
      <div class="search-title">
        查询条件
      </div>
      <div class="search-form">
        <div class="form-row">
          <div class="form-column">
            <span class="column-title">用户名称</span>
            <el-input v-model="queryParams.loginName" placeholder="请输入" clearable @keyup.enter.native="handleQuery"
              class="input-w150" />
          </div>
          <!-- <div class="form-column">
            <span class="column-title">手机号码</span>
            <el-input v-model="queryParams.phonenumber" placeholder="请输入" clearable @keyup.enter.native="handleQuery"
              class="input-w150" />
          </div> -->
          <div class="form-column">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </div>
        </div>
      </div>
    </div>


    <div class="body-container">
      <div class="operation-bar">
        <div class="bar-title">
          用户列表
        </div>
        <div class="bar-btn__wrapper">
          <div class="bar-btn" @click="openSelectUser" v-hasPermi="['system:role:add']"><i class="el-icon-plus"></i>新增
          </div>
          <div class="bar-btn" @click="cancelAuthUserAll" v-hasPermi="['system:role:remove']"><i
              class="el-icon-delete"></i>批量取消授权</div>
          <div class="bar-btn" @click="handleClose" v-hasPermi="['system:role:add']"><i class="el-icon-close"></i>关闭</div>
        </div>
      </div>


      <el-table v-loading="loading" :data="userList" stripe @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        
        <el-table-column label="用户名称" prop="userName" align="center" min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="用户昵称" prop="nickName"  min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="邮箱" prop="email"  min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="手机" prop="phonenumber"  min-width="120" :show-overflow-tooltip="true" />
        <el-table-column label="状态" align="center" prop="status"  min-width="120">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" min-width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <div class="table-option" slot-scope="scope">
              <span v-hasPermi="['system:role:remove']" class="danger" @click="cancelAuthUser(scope.row)">取消授权</span>
            </div>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
    <select-user ref="select" :roleId="queryParams.roleId" @ok="handleQuery" />
  </div>
</template>

<script>
import { allocatedUserList, authUserCancel, authUserCancelAll } from "@/api/system/role";
import selectUser from "./selectUser";

export default {
  name: "AuthUser",
  dicts: ['sys_normal_disable'],
  components: { selectUser },
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中用户组
      userIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        roleId: undefined,
        loginName: undefined,
        phonenumber: undefined
      }
    };
  },
  created () {
    const roleId = this.$route.params && this.$route.params.roleId;
    if (roleId) {
      this.queryParams.roleId = roleId;
      this.getList();
    }
  },
  methods: {
    /** 查询授权用户列表 */
    getList () {
      this.loading = true;
      allocatedUserList(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      }
      );
    },
    // 返回按钮
    handleClose () {
      const obj = { path: "/system/role" };
      this.$tab.closeOpenPage(obj);
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.userIds = selection.map(item => item.userId)
      this.multiple = !selection.length
    },
    /** 打开授权用户表弹窗 */
    openSelectUser () {
      this.$refs.select.show();
    },
    /** 取消授权按钮操作 */
    cancelAuthUser (row) {
      const roleId = this.queryParams.roleId;
      this.$modal.confirm('确认要取消该用户的角色吗？').then(function () {
        return authUserCancel({ userId: row.userId, roleId: roleId });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消授权成功");
      }).catch(() => { });
    },
    /** 批量取消授权按钮操作 */
    cancelAuthUserAll (row) {
      const roleId = this.queryParams.roleId;
      const userIds = this.userIds.join(",");
      this.$modal.confirm('是否取消选中用户授权数据项？').then(function () {
        return authUserCancelAll({ roleId: roleId, userIds: userIds });
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("取消授权成功");
      }).catch(() => { });
    }
  }
};
</script>